package com.allen.retrofit2_rxjava2.view.selectimageview.utils;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.text.format.DateFormat;
import android.util.Log;

/**
 * 图片裁剪工具类
 * 
 * @author GuiLin
 */
public class CropUtil {

	public static String cropDir = getSDCardPath() + "demo/cropImg/";
	public static StringBuilder fileName = new StringBuilder();

	/**
	 * 将bitmap保存到裁剪文件夹下
	 * 
	 * @param bitmap
	 *            保存的绝对路径+名称
	 */
	public static boolean saveBitmap2Crop(Bitmap bitmap) {
		boolean saveFlag = false;
		if (!sdCardIsExit())
			return saveFlag; // sd卡不可用
		resetStringBuilder();
		fileName.append(getCurrentTime());
		fileName.append(".jpg");
		creatDir(cropDir);
		File file = new File(cropDir, fileName.toString());
		try {
			OutputStream os = new FileOutputStream(file);
			bitmap.compress(CompressFormat.JPEG, 100, os);
			saveFlag = true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return saveFlag;
	}

	public static boolean saveFileToSdcard(byte[] b) throws IOException {
		boolean saveFlag = false;
		if (!sdCardIsExit())
			return saveFlag; // sd卡不可用
		resetStringBuilder();
		fileName.append("head.jpg");
		// fileName.append(".jpg");
		creatDir(cropDir);
		File file = new File(cropDir, fileName.toString());
		FileOutputStream fout = new FileOutputStream(file);

		BufferedOutputStream bStream = new BufferedOutputStream(fout);

		Log.e("File", "---------------------1");
		// 将字节写入文件
		// fout.write(b);
		// for (int i = 0; i < b.length; i++) {
		// byte c = b[i];
		// bStream.write(c);
		// }
		bStream.write(b);
		saveFlag = true;
		Log.e("File", "---------------------2");
		bStream.close();
		fout.close();
		return saveFlag;
	}

	/**
	 * 获取图片真实路径
	 * 
	 * @param
	 * @return
	 */
	public static String getRealFilePath() {
		return cropDir + fileName.toString();
	}

	/**
	 * 获取当前时间<br>
	 */
	private static CharSequence getCurrentTime() {
		return DateFormat.format("yyyy-MM-dd-hh-mm-ss", new Date());
	}

	/**
	 * 重置fileName
	 */
	private static void resetStringBuilder() {
		if (fileName.length() > 0) {
			fileName.delete(0, fileName.length());
		}
	}

	/**
	 * 创建文件夹
	 */
	private static String creatDir(String dirPath) {
		File file = new File(dirPath);
		if (!file.exists()) {
			file.mkdirs();
		}
		return dirPath;
	}

	/**
	 * 判断SD卡是否可用
	 */
	private static boolean sdCardIsExit() {
		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
	}

	/**
	 * 获取SD卡路径
	 */
	private static String getSDCardPath() {
		if (sdCardIsExit()) {
			return Environment.getExternalStorageDirectory().toString() + "/";
		}
		return null;
	}

	public static byte[] getBitmapByte(Bitmap bitmap) {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		bitmap.compress(CompressFormat.JPEG, 100, out);
		try {
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return out.toByteArray();
	}

	public static Bitmap getBitmapFromByte(byte[] temp) {
		if (temp != null) {
			Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);
			return bitmap;
		} else {
			return null;
		}
	}
}
